﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class AddNewEmployee : Form, IValidating
    {
        public AddNewEmployee()
        {
            InitializeComponent();
        }

        public AddNewEmployee(DataRow emp, DataRow con) : this()
        {
            _employee = emp;
            _contract = con;
        }
        
        internal DataRow _employee;
        internal DataRow _contract;


        private void formAddNewEmployee_Load(object sender, EventArgs e)
        {
            // job details
            var positions = new List<StaffRole>
                                {
                                    new StaffRole(Position.StoreManager),
                                    new StaffRole(Position.AssistantManager),
                                    new StaffRole(Position.StoreDeputy),
                                    new StaffRole(Position.StoreAssistant),
                                    new StaffRole(Position.Trainee)
                                };

            cbPosition.DataSource = positions;
            cbPosition.DisplayMember = "PositionName";
            cbPosition.ValueMember = "position";

            btnClear_Click(null, null);
        }
                
        void FillEmpRow()
        {
            UpdateEmployee.FillRow(_employee,
                tbFirstName,
                tbLastName,
                rbMale,
                tbPhoneNumber,
                tbMobilePhone,
                tbAddress,
                tbNIN,
                dpBirthDate);            
        }

        void FillContractRow()
        {
            ContractDetails.FillRow(_contract,
                dtStartingDate,
                true,
                cbPosition,
                cbOffProd.Checked,
                nudContractHrs,
                nudHolidays,
                tbComments);                        
        }
        

        /// <summary>
        /// sets default values for all fields
        /// </summary>        
        private void btnClear_Click(object sender, EventArgs e)
        {
            rbMale.Checked = true;
            cbOffProd.Checked = false;
            cbPosition.SelectedIndex = -1;

            var controlsToClear = tblPersonalDetails.Controls.Cast<Control>().ToList();
            controlsToClear.AddRange(tblJobDetails.Controls.Cast<Control>());
            controlsToClear.Add(tbComments);
            foreach (var c in controlsToClear)
            {
                if ((c.GetType() == typeof(TextBox)) ||
                    (c.GetType() == typeof(MaskedTextBox)))
                {
                    c.Text = String.Empty;
                }
                if (c.GetType() == typeof(NumericUpDown))
                {
                    var cas = c as NumericUpDown;
                    cas.Value = 0;
                }
                if (c.GetType() == typeof(DateTimePicker))
                {
                    var cas = c as DateTimePicker;
                    if (cas.Tag.ToString() == "c")
                    {
                        cas.Value = DateTime.Now.Date;
                    }
                    if (cas.Tag.ToString() == "e")
                    {
                        cas.Value = DateTime.Now.Date.AddYears(-20);
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var validated = DoesValidate();

            // validation fails
            if (validated.Key == FormValidationResult.Error)
            {
                var message = "Important data is missing:\n" + validated.Value;
                message += "Please fill up the form correctly and try again.";
                MessageBox.Show(message, Resources.MESSAGEBOX_ERROR_ValidationProblem_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return; // fail
            }

            // validation succeeds if user accepts the warning
            if (validated.Key == FormValidationResult.Fine)
            {
                var message = "Data incomplete or doubtful:\n" + validated.Value;
                message += "Do you want to add the employee anyway?";
                
                var decision = MessageBox.Show(
                    message,
                    Resources.MESSAGEBOX_QUESTION_FormValidationResultPROBLEM_CAPTION,
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question);
                if (decision == DialogResult.Cancel)
                {
                    return; // user didn't accept; fail
                }
            }

            FillEmpRow();
            FillContractRow();
            
            DialogResult = DialogResult.OK; // either no problem at all, or user has acknowledged our warning
        }

        
        /// <summary>
        /// validates the input data
        /// </summary>
        /// <returns>key: 1 if OK, 0 if warning, -1 if invalid. Value: error pMessage</returns>
        public KeyValuePair<FormValidationResult, string> DoesValidate()
        {            
            var message = String.Empty;
            var validateEmp = UpdateEmployee.DoesValidate(
                tbFirstName,
                tbLastName,
                rbMale,
                rbFemale,
                tbPhoneNumber,
                tbMobilePhone,
                tbAddress,
                tbNIN,
                dpBirthDate);
            var validateCont = ContractDetails.DoesValidate(cbPosition, nudContractHrs);
            
            // assigns the BIGGER (more severe warning level) value to validated
            var validated = (validateEmp.Key == FormValidationResult.Error ||
                             validateCont.Key == FormValidationResult.Error)
                                ? FormValidationResult.Error
                                : (validateEmp.Key == FormValidationResult.Problem ||
                                   validateCont.Key == FormValidationResult.Problem)
                                      ? FormValidationResult.Problem
                                      : FormValidationResult.Fine;
            message += validateEmp.Value + validateCont.Value;
        
            return new KeyValuePair<FormValidationResult, string>(
                validated,
                message);
        }
        

        
    }
}
